package com.tmobile.phonebook;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;

import com.tmobile.phonebook.exceptions.BusinessException;


public class PhoneBookClient {

	private static BufferedReader console = new BufferedReader (new InputStreamReader(System.in));
	private static PhoneBook p = new PhoneBook();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		DisplayPhoneBookTask t = new DisplayPhoneBookTask(p);
		Timer timer = new Timer();
		timer.schedule(t, 60 * 1000, 60 * 1000);
		String input = null;
		
		while (true)
		{
			System.out.println("");
			System.out.println("--------------------------------------------------------");
			System.out.println("----------tMobile Phone Book Menu-----------------------");
			System.out.println("--------------------------------------------------------");
			System.out.println("1.Add a Subscriber");
			System.out.println("2.Retrieve an existing Subscriber");
			System.out.println("3.Remove an existing Subscriber");
			System.out.println("4.Update an existing Subscriber's balance");
			System.out.println("5.Retrieve an existing Subscriber's balance");
			System.out.println("6.Exit the system!");
			System.out.print("Enter a number (1-6):");
			
			try {
				
				input = console.readLine();
				
				if (!(input.length() == 1 && Character.isDigit(input.charAt(0))))
				{
					System.out.println("Invalid entry...Enter 1-6 (included)...retry");
					continue;
				}
				
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Input failed!. Try again...");
			}
			
			int choice = Integer.parseInt(input);
			
			switch (choice)
			{
			case 1:
				readAndCreateSubscriber();
				break;
			case 2:
				getOrDeleteSubscriber(false);
				break;
			case 3:
				getOrDeleteSubscriber(true);
				break;	
			case 4:
				updateBalance();
				break;
			case 5:
				printRemainingBalanceAndMinutes();
				break;
			case 6:
				System.exit(0);
			default: continue;
			}
		}
	}

	public static void readAndCreateSubscriber()   
	{
		try 
		{
			System.out.print("Enter Phone Number:");
			long phoneNumber = Long.parseLong(console.readLine());
			
			System.out.print("Enter Name:");
			String name = console.readLine();
			
			System.out.print("Enter balance:");
			float balance = Float.parseFloat(console.readLine());
			
			System.out.print("Enter decrementRate:");
			float decrementRate = Float.parseFloat(console.readLine());
			
			p.addSubscriber(phoneNumber, name, balance, decrementRate);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch (BusinessException e) 
		{
			System.out.println("Error " + e.getErrorCode() + ":" + e.getLocalizedMessage());
			//e.printStackTrace();
		}
		
	}
	
	public static void getOrDeleteSubscriber(boolean delete) 
	{
		try 
		{
			System.out.print("Enter Phone Number:");
			long phoneNumber = Long.parseLong(console.readLine());
			
			if (delete)
				System.out.println("Subscriber removed:" + p.removeSubscriber(phoneNumber));
			else
				System.out.println("Subscriber Retreived:" + p.getSubscriber(phoneNumber));
			
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch (BusinessException e) 
		{
			System.out.println("Error " + e.getErrorCode() + ":" + e.getLocalizedMessage());
			//e.printStackTrace();
		}
		catch (NumberFormatException e) 
		{
			System.out.println(e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	public static void updateBalance() 
	{
		try 
		{
			System.out.print("Enter Phone Number:");
			long phoneNumber = Long.parseLong(console.readLine());
			
			System.out.print("Enter balanceIncrement:");
			float balanceIncrement = Float.parseFloat(console.readLine());
			
			p.updateBalance(phoneNumber, balanceIncrement);
			System.out.println("Subscribers Balance Updated!");
			
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch (BusinessException e) 
		{
			System.out.println("Error " + e.getErrorCode() + ":" + e.getLocalizedMessage());
			//e.printStackTrace();
		}
		
	}
	
	public static void printRemainingBalanceAndMinutes() 
	{
		try 
		{
			System.out.print("Enter Phone Number:");
			long phoneNumber = Long.parseLong(console.readLine());
			
			System.out.println(p.getRemainingBalanceAndMinutes(phoneNumber));
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch (BusinessException e) 
		{
			System.out.println("Error " + e.getErrorCode() + ":" + e.getLocalizedMessage());
			//e.printStackTrace();
		}
	}
	
}
